package org.genntii.warehousesystem.controller;


import com.aliyuncs.exceptions.ClientException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.genntii.warehousesystem.common.Result;
import org.genntii.warehousesystem.domain.dto.GoodsDTO;
import org.genntii.warehousesystem.domain.entity.Goods;
import org.genntii.warehousesystem.domain.vo.GoodsVO;
import org.genntii.warehousesystem.service.GoodsService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Tag(name = "货品接口")
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    @Operation(summary = "新建货品")
    @PostMapping("/v1/add")
    public Result add(@RequestBody GoodsDTO dto){
        goodsService.add(dto);
        return Result.success();
    }

    @Operation(summary = "启用货品",description = "启用物品相当于物品为正式货品")
    @Parameter(name = "id",description = "货品id",required = true,in = ParameterIn.QUERY)
    @PostMapping("/v1/enable")
    public Result enable(@RequestParam("id")int id){
        goodsService.enable(id);
        return Result.success();
    }

    @Operation(summary = "禁用物品",description = "禁用货品相当于软删除这件货品")
    @Parameter(name ="id",description = "货品id",required = true,in = ParameterIn.QUERY)
    @PostMapping("/v1/unable")
    public Result unable(@RequestParam("id")int id){
        goodsService.unable(id);
        return Result.success();
    }

    @Operation(summary = "设为在售",description = "将货品设为在售状态")
    @Parameter(name = "id",description = "货品id",required = true,in = ParameterIn.QUERY)
    @PostMapping("/v1/onSale")
    public Result onSale(@RequestParam("id")int id){
        goodsService.onSale(id);
        return Result.success();
    }

    @Operation(summary = "停售物品",description = "将物品设为停售代表该物品因某种原因暂时无法售卖")
    @Parameter(name = "id",description = "货品id",required = true,in = ParameterIn.QUERY)
    @PostMapping("/v1/unSale")
    public Result unSale(@RequestParam("id")int id){
        goodsService.unSale(id);
        return Result.success();
    }

    @Operation(summary = "修改货品",description = "修改货品信息")
    @Parameter(name = "id",description = "货品id",required = true,in = ParameterIn.QUERY)
    @PostMapping("/v1/update")
    public Result update(@RequestParam("id")int id,@RequestBody GoodsDTO dto){
        goodsService.update(id,dto);
        return Result.success();
    }

    @Operation(summary = "分页展示",description = "将货品按照分页的形式展示")
    @Parameter(name = "page",description = "页码",required = true,in = ParameterIn.QUERY)
    @GetMapping("list")
    public Result<List<GoodsVO>> findByPage(@RequestParam("page")int page){
        return Result.success(goodsService.findByPage(page));
    }

    @Operation(summary = "搜索")
    @Parameters({
            @Parameter(name = "word",description = "搜索关键词",required = true,in = ParameterIn.QUERY),
            @Parameter(name = "page",description = "页码",required = true,in = ParameterIn.QUERY)
    })
    @GetMapping("search")
    public Result<List<GoodsVO>> search(@RequestParam("word")String word,@RequestParam("page")int page){
        return Result.success(goodsService.searchPage(word,page));
    }

    @Operation(summary = "货品详情")
    @Parameter(name = "id",description = "货品id",required = true,in = ParameterIn.QUERY)
    @GetMapping("details")
    public Result<Goods> details(@RequestParam("id") int id){
        return Result.success(goodsService.GoodsDetails(id));
    }

    @Operation(summary = "设置商品图片")
    @Parameter(name = "id",description = "货品id",required = true,in = ParameterIn.QUERY)
    @PostMapping("/v1/setImg")
    public Result setImg(@RequestParam("id")int id,MultipartFile file) throws IOException, ClientException {
        goodsService.setImg(id,file);
        return Result.success();
    }

}
